<?php

class News extends Core {	
	public $error;
	
	public function all($top = 5) {
		$rs = $this->Database->query('SELECT TOP ' . $top . ' *,
			 (SELECT COUNT(1) FROM Mithos.dbo.news_comments WHERE Mithos.dbo.news_comments.news_id = Mithos.dbo.news.id) AS count_comments 
			 FROM Mithos.dbo.news 
			 ORDER BY id DESC
		')->fetch_all();
		$data = array();
		foreach ($rs as $key => $value) {
			$data[$key] = $value;
			$data[$key]['title'] = html_entity_decode(stripslashes($data[$key]['title']));
			$data[$key]['body'] = html_entity_decode(stripslashes($data[$key]['body']));
			$data[$key]['created'] = date('d-m-Y H:i', strtotime($data[$key]['created']));
			$data[$key]['modified'] = date('d-m-Y H:i', strtotime($data[$key]['modified']));
		}
		return $data;		
	}
	
	public function get($id) {
		$rs = $this->Database->query('SELECT * 
			FROM Mithos.dbo.news 
			WHERE id = %s
		', $id)->fetch();
		$data = array();
		if (!empty($rs)) {
			$data = $rs;
			$data['title'] = html_entity_decode(stripslashes($data['title']));
			$data['body'] = html_entity_decode(stripslashes($data['body']));
			$data['created'] = date('d-m-Y H:i', strtotime($data['created']));
			$data['modified'] = date('d-m-Y H:i', strtotime($data['modified']));
			$data['comments'] = $this->get_comments($id);
		}
		return $data;
	}
	
	public function get_comments($id) {
		$rs = $this->Database->query('SELECT * 
			FROM Mithos.dbo.news_comments 
			WHERE news_id = %s AND display = 1 
			ORDER BY id DESC
		', $id)->fetch_all();
		$data = array();
		foreach ($rs as $key => $value) {
			$data[$key] = $value;
			$data[$key]['body'] = html_entity_decode(stripslashes($data[$key]['body']));
			$data[$key]['avatar'] = Convert::avatar($this->Character->get($data[$key]['author'], 'name'));
		}
		return $data;		
	}
	
	public function post_comment($post) {
		$character = isset($post['character']) ? htmlentities(trim(strip_tags($post['character']))) : '';
		$comment = isset($post['comment']) ? htmlentities(trim(strip_tags($post['comment']))) : '';
		$news_id = isset($post['news_id']) ? (int)$post['news_id'] : 0;
		if (Validate::blank($character)) {
			$this->error = 'Campo personagem foi deixado em branco.';
		} elseif (Validate::blank($comment)) {
			$this->error = 'Campo coment&aacute;rio foi deixado em branco.';
		} elseif (!Validate::min($comment, 10)) {
			$this->error = 'Seu coment&aacute;rio deve conter no m&iacute;nimo 10 characteres.';
		} else {
			$values = array(
				'author' => $character,
				'created' => date('Y-m-d H:i:s'),
				'body' => nl2br($comment),
				'news_id' => $news_id
			);
			$this->Database->query('INSERT INTO 
				Mithos.dbo.news_comments 
				(author, created, body, news_id, display) 
				VALUES 
				(%s, %s, %s, %s, 1)
			', $character, date('Y-m-d H:i:s'), nl2br($comment), $news_id);
			return $this->last_comment();
		}
		return false;
	}
	
	public function last_comment() {
		$rs = $this->Database->query('SELECT 
			TOP 1 * 
			FROM Mithos.dbo.news_comments 
			ORDER BY id DESC
		')->fetch();
		$data = array();
		if (!empty($rs)) {
			$data = $rs;
			$data['body'] = html_entity_decode(stripslashes($data['body']));
			$data['avatar'] = Convert::avatar($this->Character->get($data['author'], 'name'));
		}
		return $data;
	}
}

?>